<?php
class Marticle extends CI_Model {
    protected $_table;
    public function __construct() {
        parent::__construct();
        $this->_table="articles";
    }
    
    public function listAll($amount, $start) {
        $query=$this->db->query("select a.*, u.u_username, c.cate_title, s.status_name from articles as a
                            inner join users as u on u.u_id = a.u_id
                            inner join categories as c on c.cate_id = a.cate_id
                            inner join status as s on s.status_id = a.status_id
                            limit $start, $amount");
        if($query->num_rows()>0) return $query->result_array(); else return false;
    }
    
    public function countAll() {
        return $this->db->get($this->_table)->num_rows();
    }
    
    public function search($title, $datefrom, $dateto, $author, $username, $cate, $status, $amount, $start) {
        $t=$d=$a=$u=$c=$s="'1=1'";
        if($title!="") $t="art_title like '%".$title."%'";
        if($datefrom!="" and $dateto!="") $d="art_date between '".$datefrom."' and '".$dateto."'";
        if($author!="") $a="art_author like '%".$author."%'";
        if($username!="") $u="u.u_username like '%".$username."%'";
        if($cate!=0) $c="a.cate_id = $cate";
        if($status!=0) $s="a.status_id = $status";
        $str="select a.*, u.u_username, c.cate_title, s.status_name from articles as a
                inner join users as u on u.u_id = a.u_id
                inner join categories as c on c.cate_id = a.cate_id
                inner join status as s on s.status_id = a.status_id
                where $t and $d and $a and $u and $c and $s
                limit $start, $amount";
        $query=$this->db->query($str);
        if($query->num_rows()>0) return $query->result_array(); else return false;
    }
    
    public function countAllResults($title, $datefrom, $dateto, $author, $username, $cate, $status) {
        $t=$d=$a=$u=$c=$s="'1=1'";
        if($title!="") $t="art_title like '%".$title."%'";
        if($datefrom!="" and $dateto!="") $d="art_date between '".$datefrom."' and '".$dateto."'";
        if($author!="") $a="art_author like '%".$author."%'";
        if($username!="") $u="u.u_username like '%".$username."%'";
        if($cate!=0) $c="a.cate_id = $cate";
        if($status!=0) $s="a.status_id = $status";
        $str="select count(*) as 'count' from articles as a
                inner join users as u on u.u_id = a.u_id
                inner join categories as c on c.cate_id = a.cate_id
                inner join status as s on s.status_id = a.status_id
                where $t and $d and $a and $u and $c and $s";
        $query=$this->db->query($str);
        if($query->num_rows()>0) return $query->row_array(); else return false;
    }
    
    public function getById($id) {
        $this->db->where("art_id", $id);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0) return $query->row_array(); else return false;
    }
    
    public function getByCateIdAndStatusId($cate_id, $status_id=6) {
        $this->db->where("cate_id", $cate_id);
        $this->db->where("status_id", $status_id);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0) return $query->row_array(); else return false;
    }
    
    public function insert($data) {
        $this->db->insert($this->_table, $data);
    }
    
    public function update($data, $id) {
        $this->db->where("art_id", $id);
        $this->db->update($this->_table, $data);
    }
    
    public function del($id) {
        $this->db->where("art_id", $id);
        $this->db->delete($this->_table);
    }
}
?>